#include "TextureManager.h"
#include "../Renderer/D3D11Renderer.h"
#include "DDSTextureLoader.h"
using namespace DirectX;

vector<Image>		TextureManager::textures;

TextureManager::TextureManager()
{

}

TextureManager::TextureManager(const TextureManager& _textureManager)
{

}

TextureManager::~TextureManager()
{
	Shutdown();
}

int TextureManager::AddTexture(WCHAR* _filename)
{
	HRESULT hr;
	CComPtr<ID3D11ShaderResourceView> srv;
	CComPtr<ID3D11Resource> texture;
	
	//Load in the texture
	hr = CreateDDSTextureFromFile(D3D11Renderer::d3dDevice, _filename, &texture, &srv);
	if(FAILED(hr))
	{
		MessageBox(NULL, "Cannot Find Texture File : TextureManager::AddTexture(ID3D11Device* _device, WCHAR* _filename)", "Texture Error", 0);
		return -1;
	}

	D3D11_TEXTURE2D_DESC texDesc;
	ZeroMemory(&texDesc, sizeof(D3D11_TEXTURE2D_DESC));
	Image temp;
	temp.texture = srv;
	((ID3D11Texture2D*)texture.p)->GetDesc(&texDesc);

	temp.height = (float)texDesc.Height;
	temp.width = (float)texDesc.Width;

	textures.push_back(temp);

	return textures.size() - 1;
}

ID3D11ShaderResourceView* TextureManager::GetTexture(int _index)
{
	if(_index < 0 || (unsigned int)_index > textures.size() - 1)
	{
		MessageBox(NULL, "Texture ID Out of Range : TextureManager::GetTexture(int _index)", "Texture Error", 0);
		return NULL;
	}

	return textures[_index].texture;
}

ID3D11ShaderResourceView* TextureManager::GetTextureArrayPointer()
{
	if(textures.size() <= 0)
	{
		MessageBox(NULL, "No Textures Loaded : TextureManager::GetTextureArrayPointer()", "Texture Error", 0);
		return NULL;
	}	

	return textures[0].texture;
}

int TextureManager::NumberOfTextures()
{
	return textures.size();
}

void TextureManager::Shutdown()
{
	textures.clear();

	return;
}

float TextureManager::GetTextureHeight(int _index)
{
	if(_index < 0 || (unsigned int)_index > textures.size() - 1)
	{
		MessageBox(NULL, "Texture ID Out of Range : TextureManager::GetTextureHeight(int _index)", "Texture Error", 0);
		return 0.0f;
	}

	return textures[_index].height;
}
float TextureManager::GetTextureWidth(int _index)
{
	if(_index < 0 || (unsigned int)_index > textures.size() - 1)
	{
		MessageBox(NULL, "Texture ID Out of Range : TextureManager::GetTextureWidth(int _index)", "Texture Error", 0);
		return 0.0f;
	}

	return textures[_index].width;
}